/*
 * Copyright 2008 Wink Saville
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.saville.testhmc;

import com.saville.debug.*;
import com.saville.hmc.*;
import com.saville.msgcomp.*;

public class TestHmcServer extends ActiveHmc {

    public TestHmcServer(String name) {
        super(name, 2, 0);
        df = Log.VERBOSE;
        states[0] = new State_Initial();
        states[1] = new State_Echo();

        /*
         * C A R E F U L: code after the call to completeConstruction 
         * may be executed after MC_CMD_STARTED is delivered to the
         * initial state.
         */
        completeConstruction();
        Log.d(df, "Server: %s TestHsmServer() X", mName);
    }

    private class State_Initial extends State {
        @Override
        public boolean doState(Msg msg) {
            if (msg.is(McConst.MC_GUID, McConst.MC_CMD_STARTED)) {
                Log.d(df, "Server: %s Initial STARTED", mName);
            } else {
                Log.d(df, "Server: %s Initial got 0x%x", mName, msg.cmd);
            }

            transitionTo(1);
            return true;
        }
    }

    private class State_Echo extends State {
        @Override
        public boolean doState(Msg msg) {
            if (msg.cmdIsRequest()) {
                if ((msg.cmd & McConst.NOISY) != 0)
                    Log.d(df, "Server: %s Echo echoing msg.cmd=0x%x", mName, msg.cmd);
                sendReplyMsg(msg, 0);
            } else {
                if ((msg.cmd & McConst.NOISY) != 0)
                    Log.d(df, "Server: %s SEcho NOT echoing msg.cmd=0x%x", mName, msg.cmd);
            }
            return true;
        }
    }
}
